package com.dorm.controller;

import com.dorm.entity.HygieneRecord;
import com.dorm.service.HygieneService;
import com.dorm.util.Debug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.ArrayList;

@RestController
@RequestMapping("/api/hygiene-records")
@CrossOrigin(originPatterns = "*", allowedHeaders = "*", allowCredentials = "true")
public class HygieneController {
    private final HygieneService hygieneService;

    @Autowired
    public HygieneController(HygieneService hygieneService) {
        this.hygieneService = hygieneService;
    }

    @GetMapping("/room/{roomId}")
    public ResponseEntity<List<HygieneRecord>> getByRoom(@PathVariable Long roomId) {
        try {
            Debug.log("接收到获取房间卫生记录请求, roomId: " + roomId);
            List<HygieneRecord> records = hygieneService.getByRoom(roomId);
            if (records == null) {
                records = new ArrayList<>();
            }
            Debug.log("成功获取房间卫生记录, 数量: " + records.size());
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            Debug.error("获取房间卫生记录失败:", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .header("X-Error-Message", e.getMessage())
                .build();
        }
    }

    @PostMapping
    public ResponseEntity<HygieneRecord> create(@RequestBody HygieneRecord record) {
        return ResponseEntity.ok(hygieneService.create(record));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        hygieneService.delete(id);
        return ResponseEntity.ok().build();
    }
} 